package com.LostAndFound.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.LostAndFound.domain.SystemInfo;
import com.LostAndFound.domain.User;
import com.LostAndFound.service.MeService;

public class MeServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	MeService service = new MeService();
	// update my info 
	public void editMyInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取数据
		String uid = request.getParameter("uid");
		String realname = request.getParameter("realname");
		String cellphone = request.getParameter("cellphone");
		Integer gender = Integer.parseInt(request.getParameter("gender"));
		String email = request.getParameter("email");
		String resume = request.getParameter("resume");
		// 封装数据
		User eidtUser = new User();
		eidtUser.setUid(uid);
		eidtUser.setRealname(realname);
		eidtUser.setCellphone(cellphone);
		eidtUser.setGender(gender);
		eidtUser.setEmail(email);
		eidtUser.setResume(resume);
		// 传递到 service 层
		boolean success = service.editMyInfo(eidtUser);
		// 得到修改后的user，保存到session中
		User user = null;
		user = service.findUserToSession(eidtUser.getUid());
		HttpSession session = request.getSession();
		session.setAttribute("user", user);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}
	
	public void updatePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String newPassword = request.getParameter("newPassword");
		String uid = request.getParameter("uid");
		boolean success = service.updatePassword(uid,newPassword);
		User user = null;
		user = service.findUserToSession(uid);
		HttpSession session = request.getSession();
		session.setAttribute("user", user);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("{\"success\":"+success+"}");
	}	
	public void getSystemInfoList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 得到系统信息列表
		List<SystemInfo> systemInfoList = null;
		systemInfoList = service.getSystemInfoList();
		
		HttpSession session = request.getSession();
		session.setAttribute("systemInfoList", systemInfoList);
		response.setContentType("text/html;charset=utf-8");
		response.sendRedirect("user/04/systemInfo.jsp");
	}
	public void showSystemInfoDetailByInfoId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String infoid = request.getParameter("infoid");
		SystemInfo systemInfo = new SystemInfo();
		systemInfo = service.showSystemInfoDetailByInfoId(infoid);
		HttpSession session = request.getSession();
		session.setAttribute("systemInfo", systemInfo);
		response.sendRedirect("user/04/systemInfoDetail.jsp");
	}
}